package com.zh.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @Description: 心跳检测server 端处理器
 * @ClassName MyServerHandler
 * @date: 2021.03.09 09:20
 * @Author: zhanghang
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    /**
     * @param ctx  上下文
     * @param evt  事件
     * @return void
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            // 将evt 向下转型 IdleStateEvent
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = "";
            switch (event.state()){
                case READER_IDLE: // 读空闲
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE: // 写空闲
                    eventType = "写空闲";
                    break;
                case ALL_IDLE: // 读写空闲
                    eventType = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress()+"--超时时间--"+eventType);
        }
    }
}
